<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2017-08-27
 * Time: 1:39
 */

namespace app\controllers;

//require_once '../librarys/wx_pay/lib/WxPay.Api.php';
//require_once '../librarys/wx_pay/lib/log.php';


use app\librarys\Tools;
use app\models\Ad;
use app\models\WxPay;
use CLogFileHandler;
use yii\web\Controller;
use yii\helpers\Url;

class WxPayController extends Controller
{
    public $layout = false;
    public $enableCsrfValidation = false;

    /**
     * 测试支付
     */
    public function actionWxPayTest()
    {

//        WxPay::wxPayTest();
    }

    /**
     * 广告红包支付,特殊行业广告认证信息支付回调
     */
    public function actionPayNotify()
    {

        $data = file_get_contents('php://input');
//        file_put_contents('../web/logs/' . date('YmdHis') . 'actionPayNotify.txt', $data);
        $array = Tools::FromToXml($data);

        $sign = Tools::MakeSign($array, \Yii::$app->params['wx_pay_key']);

//        $array['sign'] = $sign;
        if ($array['sign'] == $sign) {
            if ($array['result_code'] == 'SUCCESS' && $array['return_code'] == 'SUCCESS') {
                try {
                    $result = false;
                    $type = $array['attach'];
                    //2--特殊行业广告认证费用 3--广告红包费用支付
                    if ($type == 2) {
                        $result = WxPay::modifyAdSpecialPay($array);
                    } else if ($type == 3) {
                        $result = WxPay::modifyAdCashGiftPay($array);
                    }

                    if ($result) {
                        $echostr = $this->_toXml("1");
                        echo $echostr;
                    }
                } catch (\Exception $e) {
                    file_put_contents('../web/logs/' . date('YmdHis') . 'pay-notify-error.txt', $e->getMessage());
                }
            }
        }
    }

    /**
     * 平台入驻支付回调
     */
    public function actionPayMerchantGroupNotify()
    {
        $data = file_get_contents('php://input');
//        file_put_contents('../web/logs/' . date('YmdHis') . 'PayMerchantGroupNotify.txt', $data);
        $array = Tools::FromToXml($data);

        $sign = Tools::MakeSign($array, \Yii::$app->params['wx_pay_key']);
        if ($array['sign'] == $sign) {
            if ($array['result_code'] == 'SUCCESS' && $array['return_code'] == 'SUCCESS') {
                try {
                    $result = WxPay::modifyMerchantGroupPay($array);
                    if ($result) {
                        $echostr = $this->_toXml("1");
                        echo $echostr;
                    }
                } catch (\Exception $e) {
                    file_put_contents('../web/logs/' . date('YmdHis') . 'pay-merchan-group-notift-error.txt', $e->getMessage());
                }
            }
        }
    }

    private function _toXml($isok)
    {
        $re = 'FAIL';
        if ($isok == '1') {
            $re = 'SUCCESS';
            $message = 'OK';
        }
        $res = "<xml>
              <return_code><![CDATA[$re]]></return_code>
              <return_msg><![CDATA[$message]]></return_msg>
            </xml>";
        return $res;
    }
}